/*
 * @(#)LogEventTableDaoImpl.java   1.0  2021年11月8日
 * 
 * Copyright (c)	2014-2020. All Rights Reserved.	GuangZhou hhmk Technology Company LTD.
 */
package com.swift.dts.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.swift.dao.db.springjdbc.Jdbc;
import com.swift.dts.dao.LogEventTableDao;
import com.swift.dts.dao.model.LogEventTable;
import com.swift.util.date.DateUtil;

/**
 * 添加说明 
 * @author zhengjiajin
 * @version 1.0 2021年11月8日
 */
@Component
public class LogEventTableDaoImpl implements LogEventTableDao {

    @Autowired
    private Jdbc jdbc;

    /** 
     * @see com.swift.dts.dao.LogEventTableDao#createTable(java.lang.String)
     */
    @Override
    public void createTable(String createTableSql) {
        jdbc.execute(createTableSql);
    }

    /** 
     * @see com.swift.dts.dao.LogEventTableDao#addPartition()
     */
    @Override
    public void addPartition() {
        //建立连续3天的表分区
        for(int i=-3;i<3;i++) {
            try {
                Date date = DateUtil.addDays(new Date(), i);
                String dateStr = DateUtil.formatDate(date, "yyyyMMdd");
                Integer toDays = jdbc.queryForInt("SELECT TO_DAYS('"+dateStr+"')");
                String sql = "ALTER TABLE swift_dts_log_event_table ADD PARTITION(PARTITION p"+dateStr+" VALUES LESS THAN ("+toDays+"))";
                jdbc.execute(sql);
            }catch(Exception ex) {
                //不处理
            }
        }
    }

    /** 
     * @see com.swift.dts.dao.LogEventTableDao#cleanPartition()
     */
    @Override
    public void cleanPartition() {
        //保留三个分区，因为防止无SCHEMA库权限，固不查分数
        for(int i=10;i>3;i--) {
            try {
                Date date = DateUtil.addDays(new Date(), -i);
                String dateStr = DateUtil.formatDate(date, "yyyyMMdd");
                String sql = "ALTER TABLE swift_dts_log_event_table DROP PARTITION p"+dateStr;
                jdbc.execute(sql);
            }catch(Exception ex) {
                //不处理
            }
        }
    }

    /** 
     * @see com.swift.dts.dao.LogEventTableDao#updateSuccess(java.lang.Long)
     */
    @Override
    public void updateSuccess(Long sqlId) {
        String sql = "update swift_dts_log_event_table set state=1,update_time=NOW() where sql_id="+sqlId;
        jdbc.update(sql);
    }

    /** 
     * @see com.swift.dts.dao.LogEventTableDao#listBySqlKey()
     */
    @Override
    public List<LogEventTable> listBySqlKey(String sqlKey) {
        String sql = "SELECT * FROM swift_dts_log_event_table where create_time>=DATE_SUB(NOW(),INTERVAL 1 DAY) AND sql_key=?";
        return jdbc.queryForList(sql, LogEventTable.class, sqlKey);
    }

    /** 
     * @see com.swift.dts.dao.LogEventTableDao#save(com.swift.dts.dao.model.LogEventTable)
     */
    @Override
    public Long save(LogEventTable logEventTable) {
        String sqlStr = logEventTable.getSqlStr().replace("'", "''");
        String sql = "INSERT INTO swift_dts_log_event_table(create_time,sql_key,sql_str,state) VALUES(NOW(),'"+logEventTable.getSqlKey()+"','"+sqlStr+"',0)";
        return jdbc.insertForLastId(sql);
    }
    
    
}
